﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yamfinder.Data;
using Yamfinder.Service;
using Yamfinder.Web.Models;

namespace Yamfinder.Web.Controllers
{
    public class SearchController : Controller
    {
        private readonly ILexicalService _lexicalService;
        private readonly IDataTypeService _dataTypeService;

        public SearchController(ILexicalService lexicalService, IDataTypeService dataTypeService)
        {
            _lexicalService = lexicalService;
            _dataTypeService = dataTypeService;
        }

        //
        // GET: /Search/

        public ActionResult Index()
        {
            var fields = _dataTypeService.Search(new DataTypeSearchCriteria() {OnlyRequired = false});

            var criteria = new LexicalSearchCriteria();
            criteria.XAxisFieldname = "Language";
            criteria.XAxisSortIncreasing = true;
            criteria.YAxisFieldname = "English";
            criteria.YAxisSortIncreasing = true;

            //criteria.Filters.Add(new LexicalFilter("TestFieldText", FilterType.TextOnlyExactMatch).AddFilterData("data", "hamsters are fun"));
            var startTime = DateTime.Now;

            var results = _lexicalService.Search(criteria);

            var fieldsToShow = criteria.ExtraFields;
            fieldsToShow.Add("Orthographic");
            fieldsToShow = fieldsToShow.Distinct().ToList();

            var model = new SearchResultsModel(startTime, results, fieldsToShow);

            return View(model);
        }

    }
}
